<?php

namespace app\service;

use app\exception\CommonException;
use app\model\Template;
use app\plugins\email\Mailer;

//注册发送验证码
const REGISTER_MARK = "REGISTER";
class TemplateService extends BaseService
{

    private $mask = "";
    private $address = "";
    private $replace = [];

    protected function setModel(): string
    {
        return Template::class;
    }

    public function setMask($mask)
    {
        $this->mask = $mask;
        return $this;
    }

    public function setAddress($address)
    {
        $this->address = $address;
        return $this;
    }

    public function setReplace($replace)
    {
        $this->replace = $replace;
        return $this;
    }

    public function sendEmail()
    {
        $template_info = $this->getOne(["mark" => $this->mask, "status" => self::STATE_ACCESS], "title,email_content");
        if (empty($template_info)) {
            throw new CommonException("发送模板不存在");
        }
        $this->replace = array_merge($this->replace, ["company" => config("mailer.company")]);
        try {
            $email = new Mailer();
            $email->to($this->address)
                ->replyTo("no-reply@example.com")
                ->subject($template_info["title"])
                ->html($template_info["email_content"], $this->replace)
                ->send();
        } catch (\Exception $e) {
              throw new CommonException($e->getMessage());
        }

    }


    public function sendSms()
    {

    }
}